<?php 
session_start();
require_once ("class/m_user.php");

	$m = new M_user();
	$user=$_SESSION['username'];
	
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'detail'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				//$id=1;
				echo json_encode($m->getDataDetail($id));
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']== 'add'){
			$ar= $_POST['var'];
			$ar["CreatedBy"]=$user;
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			
			if ($m->checkUserName($ar["UserName"])=="0"){
				echo "error";
			}else{
				$id=$m->InsertData($ar);
				echo "<tr>".genTable($m->getDataDetail($id), $_POST['rows']+1 )."</tr>"; 
			}
		}else if ($_POST['m']=='edit'){
			if (isset($_POST['var'])){
				$ar=$_POST['var'];
				$flag=isset($ar);
				$ar["UpdatedBy"]=$user;
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				
				$sqlScript="select * from tb_user where 1=1 and UserID='". $_POST['id'] ."'";
				//echo $sqlScript;
				$m->updateData($sqlScript, $ar);
				
			    echo genTable($m->getDataDetail($_POST['id']), $_POST['rows']);
			}
					
		}else if ($_POST['m']=='del'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$r=$m->delData($o["id"]);

			}
		}else if ($_POST['m']== 'search'){
			$name=$_POST['name'];
			$group=$_POST['group'];
			$branch=$_POST['branch'];
			$position=$_POST['position'];
			$page=$_POST['page'];
			$sms=$_POST['sms'];
			
			$page=isset($page)?$page:1;	
			$result=$m->getData($name,$position,$group,$branch,$sms,$page);
			$rows=$m->getRowCount();
			$i=1 + (($page-1) * $m->getListPage());
			$html="";
			if ($rows > 0){
				while($row = $result->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";    
					}
					$html .= genTable($row,$i)."</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan=9 align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
			echo json_encode( $ar_result );
			
		}else if ($_POST['m']=='check_ho'){
			$bid=$_POST['bid'];
			//1:Header Office
			$val=$m->checkBranch($bid);
			echo json_encode($val);
		}
	}else{ 
		if (isset($_GET['c']) && $_GET['c'] !="" ){
			echo json_encode( $m->getDataCBO($_GET['c']));
		}
	}

	function genTable($row,$i){
	global $userScreen;
		$s.="<td align='center'>".$i."</td>" .
			"<td style='display:none'>".$row["UserID"]."</td>" .
			"<td align='center'>".$row["UserName"]."</td>" .
			"<td align='center'>".$row["FirstName"].' '.$row["LastName"]."</td>" .
			"<td align='center'>".$row["Position"]."</td>" .
			"<td align='center'>".$row["Branch"]."</td>" .
			"<td align='center'>".$row["Group"]."</td>" .
			"<td align='center'>".$row["Status"]."</td>" ;
		if ($_SESSION["permission"][$userScreen]["Edit"]=="1") 
			$s.="<td align='center'><img src='./images/edit.png' class='btnEdit' style='cursor:pointer' title='Edit' border='0'/></td>";
		else 
			$s.="<td>&nbsp;</td>";
			
		if ($_SESSION["permission"][$userScreen]["Deleted"]=="1") 	
			$s.="<td  align='center'><img src='./images/remove.png' class='btnDel' style='cursor:pointer' title='Delete' border='0'/></td>" ;
		else
			$s.="<td>&nbsp;</td>";
			
		return $s;
	}

?>